package net.joke.base.systems;

import net.joke.base.components.Position;
import net.joke.base.components.Renderable;
import net.joke.base.components.Stats;

import org.anddev.andengine.entity.primitive.Rectangle;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import com.artemis.utils.Bag;

/** Si occupa del rendering, ovvero della visualizzazione su AndEngine degli sprite 
 * e del loro corretto posizionamento.<br> 
 * @author Gianluca
 *
 */
public class RenderSystem extends EntityProcessingSystem {

	public RenderSystem() {
		super(Renderable.class, Position.class);
//		ghost = new Bag<Entity>();
	}

	private ComponentMapper<Position> positionMapper;
	private ComponentMapper<Renderable> renderableMapper;
	private ComponentMapper<Stats> statsMapper;
	
	@Override
	public void initialize() {
		positionMapper = new ComponentMapper<Position>(Position.class, world);
		renderableMapper = new ComponentMapper<Renderable>(Renderable.class, world);
		statsMapper = new ComponentMapper<Stats>(Stats.class, world);
	}
	
//	private Bag<Entity> ghost;
	
//	@Override
//	protected void end() {
//		// TODO Auto-generated method stub
//		ghost.clear();
//	}
//	
//	@Override
//	protected final void processEntities(ImmutableBag<Entity> entities) {
//		synchronized (entities) {
//			for (int i = 0, s = entities.size(); s > i; i++) {
//				ghost.add(entities.get(i));
//			}
//		}
//		for (int i = 0, s = ghost.size(); s > i; i++) {
//			process(ghost.get(i));
//		}
//	}
	
	private Position p;
	private Renderable r;
	private Stats s;
	private Rectangle rect;
	
	@Override
	protected void process(Entity arg0) {
//		if (arg0 != null && !isRemoved(arg0)) {
//			synchronized (removed) {
				p = positionMapper.get(arg0);
				r = renderableMapper.get(arg0);
//			}
			r.andRenderable.sprite.setPosition(p.x - r.andRenderable.halfX, p.y - r.andRenderable.halfY);
			s = statsMapper.get(arg0);
			if (s != null && r.andRenderable.sprite.getChildCount() == 1) {
				rect = (Rectangle)r.andRenderable.sprite.getChild(0);
				rect.setWidth(r.andRenderable.sprite.getWidth() * s.getHealthBar());
				// Health bar
				// Mana bar
			}
		}
		// r.andRenderable.update();

	@Override
	protected boolean checkProcessing() {
		// TODO Auto-generated method stub
		return true;
	}

	
	protected void removed(Entity e) {
	// TODO Auto-generated method stub
		super.removed(e);
		Renderable r = renderableMapper.get(e);
		r.dispose();
	}
	
	
//	public boolean isRemoved(Entity e) {
//		int size = removed.size();
//		for (int i=0; i< size; i++) {
//			Entity rem = removed.get(i);
//			if (e.getId() == rem.getId()) {
//				return true;
//			}
//		}
//		return false;
//	}
//	
//	private Bag<Entity> removed;
//	
//	@Override
//	protected void removed(Entity e) {
//		// TODO Auto-generated method stub
//		super.removed(e);
//		removed.add(e);
//	}
//	
//	
//	@Override
//	protected void end() {
//		// TODO Auto-generated method stub
//		removed.clear();
//		super.end();
//	}
}
